﻿#include "numerinputdialog.h"
#include "ui_numerinputdialog.h"

NumerInputDialog::NumerInputDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::NumerInputDialog)
{
    ui->setupUi(this);
    setWindowFlags (Qt::Window | Qt::FramelessWindowHint);
    setWindowModality(Qt::ApplicationModal);
    setAttribute(Qt::WA_TranslucentBackground, true);//设置窗体背景透明

    initDialog();
    initControl();
}

NumerInputDialog::~NumerInputDialog()
{
    delete ui;
}

QString NumerInputDialog::getShowString(int64_t val, int pointflag)
{
    double value = val;

    if (pointflag < 0)
    {
        pointflag = 0;
    }
    else if (pointflag > 5)
    {
        pointflag = 5;
    }

    for (int i = 1; i < pointflag; i++)
    {
        value /= 10;
    }

    QString str;
    if (pointflag == 0)
    {
        str.sprintf("%.0f", value);
    }
    else if (pointflag == 1)
    {
        str.sprintf("%.0f", value);
        str += ".";
    }
    else if (pointflag == 2)
    {
        str.sprintf("%.1f", value);
    }
    else if (pointflag == 3)
    {
        str.sprintf("%.2f", value);
    }
    else if (pointflag == 4)
    {
        str.sprintf("%.3f", value);
    }
    else // if (pointflag >= 5)
    {
        str.sprintf("%.4f", value);
    }
    return str;
}

int NumerInputDialog::checkValueRange(int64_t val, int pointflag)
{
    int i, xsws;
    int64_t min, max;
    min = m_min;
    max = m_max;

    xsws = pointflag - 1;
    if (xsws < 0)
    {
        xsws = 0;
    }
    xsws = m_afterPoint - xsws;
    if (xsws < 0)
    {
        xsws = 0;
    }

    for (i = 0; i < xsws; i++)
    {
        min /= 10;
        max /= 10;
    }

    if (val < min || val > max)
    {
        return 1;
    }
    return 0;
}

int64_t NumerInputDialog::fixValue(int64_t val, int pointflag)
{
    int i, xsws;
    int64_t min, max;
    min = m_min;
    max = m_max;

    xsws = pointflag - 1;
    if (xsws < 0)
    {
        xsws = 0;
    }
    xsws = m_afterPoint - xsws;
    if (xsws < 0)
    {
        xsws = 0;
    }

    for (i = 0; i < xsws; i++)
    {
        min /= 10;
        max /= 10;
    }

    if (val < min)
    {
        val = min;
    }
    if (val > max)
    {
        val = max;
    }

    return val;
}

int NumerInputDialog::getPointMuti(int pointflag)
{
    int muti = 1;
    for (int i = 1; i < pointflag; i++)
    {
        muti *= 10;
    }
    return muti;
}

void NumerInputDialog::showValue()
{
    ui->labelValue->setText(getShowString(m_value, m_pointFlag));

    QString str = ui->labelValue->text();

    // 修复判断数值范围时,无法正确区分小数点的问题
    int val = m_value;
    int i;
    if (m_pointFlag <= 1)
    {
        for (i=0; i<m_afterPoint; i++)
        {
            val *= 10;
        }
    }
    else
    {
        for (i=m_pointFlag-1; i<m_afterPoint; i++)
        {
            val *= 10;
        }
    }

    if (str.length() > 0 && val >= m_min && val <= m_max)
    {
        ui->buttonOk->setEnabled(true);
    }
    else
    {
        ui->buttonOk->setEnabled(false);
    }
}

void NumerInputDialog::showRange()
{
    QString min, max, def;

    if (m_afterPoint == 0)
    {
        min = getShowString(m_min, 0);
        max = getShowString(m_max, 0);
        def = getShowString(m_defVal, 0);
    }
    else
    {
        min = getShowString(m_min, m_afterPoint+1);
        max = getShowString(m_max, m_afterPoint+1);
        def = getShowString(m_defVal, m_afterPoint+1);
    }

    QString rangeStr = "(" + min + "~" + max + ")" + m_unitStr;
    ui->labelRange->setText(rangeStr);
}

void NumerInputDialog::addNum(int num)
{
    if (((m_pointFlag < 5) &&
        ((m_afterPoint == 0) || (m_pointFlag < m_afterPoint + 1)) &&
        ((m_value > -100000000) && (m_value < 100000000)) && // 修复数值太大时显示错乱的问题
        1 ) ||
            (m_defFlag == 0) ||
            0 )
    {
        int64_t val;
        int pflag;

        if (m_defFlag == 0)
        {// 第一次按数字键,从0开始计数,解决每次修改值之前都需要按C键的问题
            m_value = 0;
            m_pointFlag = 0;
            m_defFlag = 1;
        }
        val = m_value;
        pflag = m_pointFlag;

        if (num < 0)
        {
            num = 0;
        }
        else if (num > 9)
        {
            num = 9;
        }

        val *= 10;
        val += num*((m_value<0)?-1:1);

        if (pflag != 0)
        {
            pflag++;
            if (pflag > 5)
            {
                pflag = 5;
            }
        }

        //if (checkValueRange(val, pflag) == 0)
        {
            m_value = val;
            m_pointFlag = pflag;
        }
    }
}

void NumerInputDialog::changeValue(int weights)
{
    int64_t val;
    //int pflag;

    val = m_value;

    m_defFlag = 0;      // 初始值
    if (weights > -1 && weights < 1)    // 小数
    {

    }

    //pflag = m_pointFlag;
    val += weights*getPointMuti(m_pointFlag);

    //if (checkValueRange(val, pflag) == 0)
    {
      //  val = fixValue(val, m_pointFlag);
       // if (val != m_value)
        {
            m_value = val;
            showValue();
        }
    }
}

//初始化窗体
void NumerInputDialog::initDialog()
{
    setTitleStr("");
    m_value = 0;
    m_min = 0;
    m_max = 0;
    m_afterPoint = 0;
    m_defVal = 0;
    m_defFlag = 0;
    m_pointFlag = 0;
    m_unitStr.clear();

    m_leftNumberBtnList.clear();
    m_leftNumberBtnList.append(ui->buttonPlusMinus);
    m_leftNumberBtnList.append(ui->buttonClean);
    m_leftNumberBtnList.append(ui->buttonDelete);
    m_leftNumberBtnList.append(ui->buttonNum7);
    m_leftNumberBtnList.append(ui->buttonNum8);
    m_leftNumberBtnList.append(ui->buttonNum9);
    m_leftNumberBtnList.append(ui->buttonNum4);
    m_leftNumberBtnList.append(ui->buttonNum5);
    m_leftNumberBtnList.append(ui->buttonNum6);
    m_leftNumberBtnList.append(ui->buttonNum1);
    m_leftNumberBtnList.append(ui->buttonNum2);
    m_leftNumberBtnList.append(ui->buttonNum3);
    m_leftNumberBtnList.append(ui->buttonNum00);
    m_leftNumberBtnList.append(ui->buttonNum0);
    m_leftNumberBtnList.append(ui->buttonPoint);

    m_rightNumberBtnList.clear();
    m_rightNumberBtnList.append(ui->buttonDec1000);
    m_rightNumberBtnList.append(ui->buttonInc1000);
    m_rightNumberBtnList.append(ui->buttonDec100);
    m_rightNumberBtnList.append(ui->buttonInc100);
    m_rightNumberBtnList.append(ui->buttonDec10);
    m_rightNumberBtnList.append(ui->buttonInc10);
    m_rightNumberBtnList.append(ui->buttonDec1);
    m_rightNumberBtnList.append(ui->buttonInc1);
    m_rightNumberBtnList.append(ui->buttonDec01);
    m_rightNumberBtnList.append(ui->buttonInc01);
}

//初始化窗体控件，包括位置、尺寸、样式
void NumerInputDialog::initControl()
{
    //根据不同分辨率设置控件的位置和尺寸
    initResolution();
    initControlStyle();//初始化窗体控件样式
}

//初始化窗体控件为任意分辨率
void NumerInputDialog::initResolution()
{
    #define FRAME_LEFT         (203)      // 小窗口
    #define FRAME_TOP          (59)
    #define FRAME_WIGHT        (620)
    #define FRAME_HEIGHT       (479)

    #define TITLE_HEIGHT       (46)       // 标题栏高度
    #define TITLE_LEFT         (367)      // 分栏左

    #define NUM_LEFT           (21)       // 数值左
    #define NUM_TOP            (84)       // 数值
    #define NUM_WIGHT          (324)      // 数值宽度
    #define NUM_HEIGHT         (42)       // 数值高度

    double factoryX = getFactoryX();
    double factoryY = getFactoryY();

    int xval = 0;
    if(g_emResolut == resolution0804)
    {
        xval = 20;
    }

    this->resize(GLB_SCR_WIGHT*factoryX,GLB_SCR_HEIGHT*factoryY);
    this->move((GLB_SCR_WIGHT*factoryX-this->width())/2+g_mainWidgetPos.x(),(GLB_SCR_HEIGHT*factoryY-this->height())/2+g_mainWidgetPos.y());
    ui->frameBack->setGeometry(0*factoryX,0*factoryY,GLB_SCR_WIGHT*factoryX+xval,GLB_SCR_HEIGHT*factoryY);

    ui->labelTitle->setGeometry((FRAME_LEFT+GLB_EDGE_WIGHT)*factoryX,(FRAME_TOP+(TITLE_HEIGHT-GLB_TEXT_HEIGHT)/2)*factoryY,(FRAME_WIGHT-GLB_TAB_WIGHT*2)*factoryX,GLB_TEXT_HEIGHT*factoryY);
    ui->labelValue->setGeometry((FRAME_LEFT+NUM_LEFT+GLB_TAB_WIGHT)*factoryX,(FRAME_TOP+NUM_TOP+GLB_ICON_TEXT)*factoryY,(NUM_WIGHT-GLB_TAB_WIGHT*2)*factoryX,GLB_TEXT_L_HEIGHT*factoryY);
    ui->labelRange->setGeometry((FRAME_LEFT+TITLE_LEFT)*factoryX,(FRAME_TOP+NUM_TOP+NUM_HEIGHT-GLB_TEXT_HEIGHT)*factoryY,(FRAME_WIGHT-TITLE_LEFT-GLB_TAB_WIGHT*2)*factoryX,GLB_TEXT_HEIGHT*factoryY);

    int bgX,bgY;
    bgX = FRAME_LEFT + NUM_LEFT;
    bgY = FRAME_TOP + NUM_TOP+NUM_HEIGHT + GLB_EDGE_WIGHT;

    for(int i = 0; i < 5; i++)
    {
        for(int j = 0; j < 3; j ++)
        {
            m_leftNumberBtnList[i*3+j]->setGeometry((bgX+j*(GLB_LBUT_WIGHT +GLB_BAS_WIGHT))*factoryX,(bgY+i*(GLB_LBUT_HEIGHT + GLB_BAS_WIGHT))*factoryY,GLB_LBUT_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);
        }
    }

    double val = 0;

    bgX = FRAME_LEFT + TITLE_LEFT + GLB_EDGE_WIGHT;
    for(int i = 0; i < 5; i++)
    {
        for(int j = 0; j < 2; j ++)
        {
            if(j == 1)
            {
                if(g_emResolut == resolution0804)
                {
                    val = 0.01;
                }
            }
            else
            {
                val = 0;
            }
            m_rightNumberBtnList[i*2+j]->setGeometry((bgX+j*(GLB_LBUT_WIGHT +GLB_BAS_WIGHT))*(factoryX+val),(bgY+i*(GLB_LBUT_HEIGHT + GLB_BAS_WIGHT))*factoryY,GLB_LBUT_WIGHT*(factoryX),GLB_LBUT_HEIGHT*factoryY);
        }
    }

    ui->buttonOk->setGeometry((bgX+0*(GLB_LBUT_WIGHT +GLB_BAS_WIGHT))*factoryX,(bgY+5*(GLB_LBUT_HEIGHT + GLB_BAS_WIGHT)+GLB_BAS_WIGHT)*factoryY,
                              GLB_LBUT_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);
    if(g_emResolut == resolution0804)
    {
        val = 0.01;
    }
    ui->buttonCancel->setGeometry((bgX+1*(GLB_LBUT_WIGHT +GLB_BAS_WIGHT))*(factoryX+val),(bgY+5*(GLB_LBUT_HEIGHT + GLB_BAS_WIGHT)+GLB_BAS_WIGHT)*factoryY,
                                  GLB_LBUT_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);

}


//初始化窗体控件样式
void NumerInputDialog::initControlStyle()
{
    SetStyle setControlStyle;
    setControlStyle.setUiName(this->objectName());

    ui->frameBack->setStyleSheet(setControlStyle.getStyleSheet(this->objectName()));

    for(int i = 0; i < m_leftNumberBtnList.size(); i++)
    {
        m_leftNumberBtnList[i]->setStyleSheet(numButtonStyle());
        m_leftNumberBtnList[i]->setFont(fontSize_M());
    }

    for(int i = 0; i < m_rightNumberBtnList.size(); i++)
    {
        m_rightNumberBtnList[i]->setStyleSheet(numButtonStyle());
        m_rightNumberBtnList[i]->setFont(fontSize_M());
    }

    ui->labelTitle->setStyleSheet(titleTextColour());
    ui->labelTitle->setFont(fontSize_M());
    ui->labelTitle->setAlignment(Qt::AlignLeft);
    ui->labelTitle->setAlignment(Qt::AlignVCenter);

    ui->labelValue->setStyleSheet(warnTextColour());
    ui->labelValue->setFont(fontSize_L());

    ui->labelRange->setStyleSheet(noteTextColour());
    ui->labelRange->setFont(fontSize_M());
    ui->labelRange->setAlignment(Qt::AlignLeft);
    ui->labelRange->setAlignment(Qt::AlignVCenter);
    ui->buttonOk->setStyleSheet(confirmIconStyle());
    ui->buttonOk->setTopImage(setControlStyle.getSharedTopStyleSheet("buttonOk"),12);
    ui->buttonCancel->setStyleSheet(confirmIconStyle());
    ui->buttonCancel->setTopImage(setControlStyle.getSharedTopStyleSheet("buttonCancel"),12);

}

//设置标题
void NumerInputDialog::setTitleStr(QString str)
{
    ui->labelTitle->setText(str);
}

void NumerInputDialog::setUnitStr(QString str)
{
    m_unitStr = str;
//    showRange();
}

void NumerInputDialog::setValueAndRange(int64_t val, int64_t min, int64_t max, int afterpoint, int defen, int64_t defvalue)
{
    m_value = val;
    m_min = min;
    m_max = max;
    m_afterPoint = afterpoint;

    if (m_min < -999999999)
    {
        m_min = -999999999;     // 这个值看起来更舒服些
    }
    if (m_max > 999999999)
    {
        m_max = 999999999;     // 这个值看起来更舒服些
    }
    if (m_min > m_max)
    {
        double tmp = m_min;
        m_min = m_max;
        m_max = tmp;
    }

    if (m_afterPoint < 0)
    {
        m_afterPoint = 0;
    }
    if (m_afterPoint > 4)
    {
        m_afterPoint = 4;
    }

    if (m_value < m_min)
    {
        m_value = m_min;
    }
    if (m_value > m_max)
    {
        m_value = m_max;
    }

    if (m_afterPoint != 0)
    {
        m_pointFlag = m_afterPoint + 1;
    }
    else
    {
        m_pointFlag = 0;
    }

    m_defFlag = defen;
    m_defVal = defvalue;

    showRange();
    showValue();
}

int64_t NumerInputDialog::getValue()
{
    int i, j;
    int64_t val = m_value;
    i = m_pointFlag;
    j = m_afterPoint;
    if (j != 0)
    {
        j += 1;
    }

    while (i < j)
    {
        if (i != 0)
        {
            val *= 10;
        }
        i++;
    }

    return val;
}

void NumerInputDialog::on_buttonClean_clicked()
{
    m_pointFlag = 0;
    m_value = 0;
    m_defFlag = 0;
    showValue();
}

void NumerInputDialog::on_buttonNum00_clicked()
{
    addNum(0);
    addNum(0);
    showValue();
}

void NumerInputDialog::on_buttonDelete_clicked()
{
    m_defFlag = 1;

    if (m_pointFlag != 1)
    {
        int64_t val;
        int pflag;
        val = m_value;
        pflag = m_pointFlag;
        val /= 10;
        pflag--;
        if (pflag < 0)
        {
            pflag = 0;
        }
        //if (checkValueRange(val, pflag) == 0)
        {
            m_value = val;
        }
    }
    if (m_pointFlag != 0)
    {
        m_pointFlag--;
    }
    showValue();
}

void NumerInputDialog::on_buttonNum7_clicked()
{
    addNum(7);
    showValue();
}

void NumerInputDialog::on_buttonNum8_clicked()
{
    addNum(8);
    showValue();
}

void NumerInputDialog::on_buttonNum9_clicked()
{
    addNum(9);
    showValue();
}

void NumerInputDialog::on_buttonNum4_clicked()
{
    addNum(4);
    showValue();
}

void NumerInputDialog::on_buttonNum5_clicked()
{
    addNum(5);
    showValue();
}

void NumerInputDialog::on_buttonNum6_clicked()
{
    addNum(6);
    showValue();
}

void NumerInputDialog::on_buttonNum1_clicked()
{
    addNum(1);
    showValue();
}

void NumerInputDialog::on_buttonNum2_clicked()
{
    addNum(2);
    showValue();
}

void NumerInputDialog::on_buttonNum3_clicked()
{
    addNum(3);
    showValue();
}

void NumerInputDialog::on_buttonPlusMinus_clicked()
{
    int64_t val;
    val = m_value;
    val *= -1;
    //if (checkValueRange(val, m_pointFlag) == 0)
    {
        m_value = val;
        showValue();
    }
}

void NumerInputDialog::on_buttonNum0_clicked()
{
    addNum(0);
    showValue();
}

void NumerInputDialog::on_buttonPoint_clicked()
{
    if (m_pointFlag == 0 && m_afterPoint != 0)
    {
        m_pointFlag = 1;
        showValue();
    }
}

void NumerInputDialog::on_buttonDec1000_clicked()
{
    changeValue(-1000);
}

void NumerInputDialog::on_buttonInc1000_clicked()
{
    changeValue(1000);
}

void NumerInputDialog::on_buttonDec100_clicked()
{
    changeValue(-100);
}

void NumerInputDialog::on_buttonInc100_clicked()
{
    changeValue(100);
}

void NumerInputDialog::on_buttonDec10_clicked()
{
    changeValue(-10);
}

void NumerInputDialog::on_buttonInc10_clicked()
{
    changeValue(10);
}

void NumerInputDialog::on_buttonDec1_clicked()
{
    changeValue(-1);
}

void NumerInputDialog::on_buttonInc1_clicked()
{
    changeValue(1);
}

void NumerInputDialog::on_buttonDec01_clicked()
{
    if (m_afterPoint != 0)
    {

        m_defFlag = 0;
        int64_t val;
        int pflag, muti;

        int dir;
        int weights = -1;
        muti = 1;

        //dir = m_value > 0 ? 1:-1;
        dir = m_value > 0 ? 1:1;
        val = m_value;
        pflag = m_pointFlag;

        if (m_pointFlag <= 1)   // 是整数
        {
            val *= 10;
            pflag = 2;      // 转换为1位小数
        }
        else       // 小数
        {
            for (int i = 2; i < m_pointFlag; i++)
            {
                muti *= 10;
            }
        }

        val += weights * dir * muti;

        //if (checkValueRange(val, pflag) == 0)
        {
           // val = fixValue(val, pflag);
            m_value = val;
            m_pointFlag = pflag;
            showValue();
        }
    }
}

void NumerInputDialog::on_buttonInc01_clicked()
{
    if (m_afterPoint != 0)
    {

        m_defFlag = 0;
        int64_t val;
        int pflag, muti;
        int dir;
        int weights = 1;

        //dir = m_value > 0 ? 1:-1;
        dir = m_value > 0 ? 1:1;
        muti = 1;

        val = m_value;
        pflag = m_pointFlag;

        if (m_pointFlag <= 1)   // 是整数
        {
            val *= 10;
            pflag = 2;      // 转换为1位小数
        }
        else       // 小数
        {

            for (int i = 2; i < m_pointFlag; i++)
            {
                muti *= 10;
            }
        }

        val += weights * dir * muti;

        //if (checkValueRange(val, pflag) == 0)
        {
          //  val = fixValue(val, pflag);
            m_value = val;
            m_pointFlag = pflag;
            showValue();
        }
    }
}

void NumerInputDialog::on_buttonOk_clicked()
{
    done(1);
}

void NumerInputDialog::on_buttonCancel_clicked()
{
    done(0);
}
